#include "graphicspanview.h"

GraphicsPanView::GraphicsPanView(QGraphicsScene *scene, QWidget *parent)
    : QGraphicsView(scene, parent)
{
    timerID = 0;
}

void GraphicsPanView::setPanEnabled(bool enabled)
{
    if (enabled)
        timerID = startTimer(20);
    else
        killTimer(timerID);

}

void GraphicsPanView::centerView()
{
    // Set the the center point to be the center of the scene
    centerOn(scene()->sceneRect().center());
}

void GraphicsPanView::timerEvent(QTimerEvent *event)
{
    //If the mouse is within 10 pixles of the right edge and there is more map to the right
    if (QCursor::pos().x() > (width() - 10))
    {
        horizontalScrollBar()->setValue(horizontalScrollBar()->value()+10);
        emit screenPanned(QPointF(mapToScene(rect().center())));
    }

    //If the mouse is within 10 pixles of the left edge and there is more map to the left
    if (QCursor::pos().x() < 10)
    {
        horizontalScrollBar()->setValue(horizontalScrollBar()->value()-10);
        emit screenPanned(QPointF(mapToScene(rect().center())));
    }

    //If the mouse is within 10 pixles of the top edge and there is more map to the top
    if (QCursor::pos().y() < 10)
    {
        verticalScrollBar()->setValue(verticalScrollBar()->value()-10);
        emit screenPanned(QPointF(mapToScene(rect().center())));
    }

    //If the mouse is within 10 pixles of the bottom edge and there is more map below the edge
    if (QCursor::pos().y() > (height() - 10))
    {
        verticalScrollBar()->setValue(verticalScrollBar()->value()+10);
        emit screenPanned(QPointF(mapToScene(rect().center())));
    }
}

void GraphicsPanView::screenMoved(QPointF newPosition)
{
    centerOn(newPosition);
}

QPointF GraphicsPanView::getCenterPoint()
{
    return QPointF(mapToScene(rect().center()));
}

void GraphicsPanView::wheelEvent(QWheelEvent *event)
{
    // We are reimplimenting the wheel event to do nothing so
    // the scroll wheel doesn't effect the map
}

